package com.huacai.spots.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.huacai.spots.domain.Spots;
import com.huacai.spots.service.ISpotsService;
import com.huacai.common.utils.poi.ExcelUtil;
import com.huacai.common.core.page.TableDataInfo;

/**
 * 景点Controller
 *
 * @author hsj
 * @date 2025-05-11
 */
@RestController
@RequestMapping("/huacai/spots")
public class SpotsController extends BaseController {
    @Autowired
    private ISpotsService spotsService;

    /**
     * 查询景点列表
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:list')")
    @GetMapping("/list")
    public TableDataInfo list(Spots spots) {
        startPage();
        List<Spots> list = spotsService.selectSpotsList(spots);
        return getDataTable(list);
    }

    /**
     * 查询景点列表（包括景点标签）
     */
    @GetMapping("/selectSptosOrTagList")
    public AjaxResult selectSpotsOrTagList(Spots spots) {
        List<Spots> list = spotsService.selectSptosOrTagList(spots);
        return success(list);
    }

    /**
     * 导出景点列表
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:export')")
    @Log(title = "景点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Spots spots) {
        List<Spots> list = spotsService.selectSpotsList(spots);
        ExcelUtil<Spots> util = new ExcelUtil<Spots>(Spots.class);
        util.exportExcel(response, list, "景点数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Spots> util = new ExcelUtil<Spots>(Spots.class);
        util.importTemplateExcel(response, "景点数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "景点", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('huacai:spots:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Spots> util = new ExcelUtil<Spots>(Spots.class);
        InputStream inputStream = file.getInputStream();
        List<Spots> list = util.importExcel(inputStream);
        inputStream.close();
        int count = spotsService.batchInsertSpots(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取景点详细信息
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:query')")
    @GetMapping(value = "/{spotsId}")
    public AjaxResult getInfo(@PathVariable("spotsId") String spotsId) {
        return success(spotsService.selectSpotsBySpotsId(spotsId));
    }

    /**
     * 新增景点
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:add')")
    @Log(title = "景点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Spots spots) {
        return toAjax(spotsService.insertSpots(spots));
    }

    /**
     * 修改景点
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:edit')")
    @Log(title = "景点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Spots spots) {
        return toAjax(spotsService.updateSpots(spots));
    }

    /**
     * 删除景点
     */
    @PreAuthorize("@ss.hasPermi('huacai:spots:remove')")
    @Log(title = "景点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{spotsIds}")
    public AjaxResult remove(@PathVariable String[] spotsIds) {
        return toAjax(spotsService.deleteSpotsBySpotsIds(spotsIds));
    }

    /**
     * 不分页查询景点列表
     * @return
     */
    @GetMapping("/selectSpotsByList")
    public AjaxResult selectSpotsByList(){
        List<Spots> List = spotsService.selectSpotsByList();
        return AjaxResult.success(List);
    }


}
